#!/usr/bin/python3

from  book.models import *

from  account.models import *
import os
import re
import xlrd
from souti.settings import *


conf_path='/media/liuqimin/3238-6430/松鼠搜题/截图集合'    ##modify this if the data is somewhere else
conf_file=os.path.join(conf_path, '高等数学-上册-第7版-同济大学数学系.xlsx')

img_dir=os.path.join(conf_path, '高等数学-上册-第7版-同济大学数学系')


workbook = xlrd.open_workbook(conf_file)
sheet=workbook.sheet_by_index(0)

#-----processing book info------------------
bookname=sheet.cell(1,0).value
bookname_short=sheet.cell(1,1).value
author=sheet.cell(1,2).value
press=sheet.cell(1,3).value
version=sheet.cell(1,4).value
cover=sheet.cell(1,5).value
ISBN=sheet.cell(1,6).value
university=sheet.cell(1,7).value
university_short=sheet.cell(1,8).value

print('bookname=',bookname, 'bookname_short=',bookname_short)
try:
    book = Book.objects.get(ISBN=ISBN)

except:
    os.system('cp ' + os.path.join(conf_path, cover) + ' '+ MEDIA_ROOT + '/cover')
    infos=bookname+ ' '+ version + ' '+author + ' ' + university
    book=Book.objects.create(
        name=bookname,
        shortname=bookname_short,
        author=author,
        press=press,
        version=version,
        cover= 'cover/'+ cover,
        ISBN=ISBN,
        university=university,
        university_short=university_short,
        infos=infos,
    )

#--------processing chapter------------------------

for i in range(4,sheet.nrows):
    title = sheet.cell(i, 0).value

    index1=sheet.cell(i, 1).value
    index2=sheet.cell(i,2).value
    index3=sheet.cell(i,3).value
    ti_count=sheet.cell(i, 4).value
    if title=='':
        break
    if index2=='':
        index1=int(index1)
        index2=-1
        index3 = -1
        layers=1
    elif index3=='':
        index1=int(index1)
        index2=int(index2)
        index3=-1
        layers=2
    else:
        index1=int(index1)
        index2=int(index2)
        index3 = int(index3)
        layers=3


    if ti_count=='':
        ti_count =-1
    else:
        ti_count=int(ti_count)

    try:
        print('get')
        print('layers=', layers, 'ti_count=', ti_count)
        chapter = Chapter.objects.get(
            book=book,
            index1=index1,
            index2=index2,
            index3=index3,
            title=title,
            ti_count=ti_count,
            layers=layers,
        )
    except:
        print('create')
        print('layers=', layers, 'ti_count=', ti_count, index1, index2, index3)
        chapter = Chapter.objects.create(
            book=book,
            index1=index1,
            index2=index2,
            index3=index3,
            title=title,
            ti_count=ti_count,
            layers=layers,
        )
        print('i=', i)


for f in os.listdir(img_dir):

    x=f.find('_')
    if x!=-1:
        continue

    if f[0]=='a':
        continue

    ch, ti_info=f.split('-')
    ch=ch[1:].split('.')
    ti_info=ti_info.split('.')

    #ti_number=int(ti_info[0])
##process chapter
    if len(ch)==1:
        index1=int(ch[0])
        index2=-1
        index3 = -1
    elif len(ch)==2:
        index1=int(ch[0])
        index2=int(ch[1])
        index3 = -1
    elif len(ch)==3:
        index1=int(ch[0])
        index2=int(ch[1])
        index3 = int(ch[2])

##process ti info
    if len(ti_info)==2:
        ti_number = int(ti_info[0])
        xiaoti_number=-1
    elif len(ti_info)==3:
        ti_number = int(ti_info[0])
        xiaoti_number = int(ti_info[1])
    # elif len(ti_info)==4:
    #     ti_number = int(ti_info[0])
    #     xiaoti_number = int(ti_info[1])

    #GetChapter(index1,index2, index3)


    try:
        chapter = Chapter.objects.get(
            index1=index1,
            index2=index2,
            index3=index3,
            book=book,
        )
    except:
        print(f)
        print('layers=',layers,'index1=', index1)
        break

    is_sub=  os.path.exists(img_dir+'/'+'a'+f[1:])
    try:
        ti=Ti.objects.get(
            book=book,
            chapter=chapter,
            ti_number=ti_number,
            xiaoti_number=xiaoti_number,
        )

    except:
        os.system('cp '+img_dir+'/'+f+ '  ' + MEDIA_ROOT +'/ti/question/')

        ti=Ti.objects.create(
            book=book,
            chapter=chapter,
            ti_number=ti_number,
            xiaoti_number=xiaoti_number,
            question='ti/question/'+f,

        )
        #ti.question(f, File().read())
        if is_sub:
            os.system('cp ' + img_dir+'/' + 'a'+f[1:] + '  ' + MEDIA_ROOT + '/ti/answer/')
            ti.answer='ti/answer/'+'a'+f[1:]
            ti.is_title=False
        else:
            ti.is_title=True

        ti.save()

